<?php
// No direct access
defined( '_JEXEC' ) or die( 'Restricted access' );

jimport('joomla.application.component.controller');

/**
 * Communal Meals Component Administrator Controller
 */
class CommunalMealsController extends JController
{
	/**
	 * constructor (registers additional tasks to methods)
	 * @return void
	 * @access public
	 */
	public function __construct() {
		parent::__construct();
		
		// Register Extra tasks
    	$this->registerTask( 'add' , 'edit' );
	    $this->registerTask( 'save_new' , 'save' );
	}
	
	/**
	 * Method to remove an item
	 * @access public
	 */
	public function remove() {
		$viewName = JRequest::getVar('view', NULL);
		switch ($viewName) {
			case "eventgroups":
				$this->removeEventGroups();
				break;
		}
	}
	
	/**
	 * Method to remove one or more event groups
	 * @access private
	 */
	private function removeEventGroups() {
		global $option;
		$idList = JRequest::getVar('id', array(0), 'post', 'array' );
		// idList is an array of id strings
		foreach ($idList as $id) {
			$eventGroup = new cmeals_std_class();
			$eventGroup->id = $id;
			CMeals::deleteEventGroup($eventGroup);
		}
		$this->setRedirect("index.php?option=".$option."&view=eventgroups" );
	}
	
	/**
	 * Method to add or edit an item
	 */
	public function edit() {
		$viewName = JRequest::getVar( 'view', NULL);
		switch ($viewName) {
			case "eventgroups":
				$this->editEventGroup();
				break;
		}
	}
	
	/**
	 * Method to add or edit an event group
	 */
	private function editEventGroup() {
		JRequest::setVar( 'view', 'editeventgroup' );
		parent::display();
	}
	
	/**
	 * Method to save an item
	 */
	public function save() {
		$viewName = JRequest::getVar('view', NULL);
		switch ($viewName) {
			case "editeventgroup":
				$this->saveEventGroup();
				break;
		}
	}
	
	/**
	 * Method to save an event group
	 */
	private function saveEventGroup() {
		global $option;
		$createMethod = JRequest::getVar( 'createmethod', NULL );
		$isNew = ($this->getTask() == "save_new");
		if ($isNew && $createMethod == "CMWK") {
			$year = JRequest::getVar('year', NULL);
			$week = JRequest::getVar('week', NULL);
			$mealDays = JRequest::getVar('mealdays', NULL);
			$crewNames = JRequest::getVar('crewnames', NULL);
			if ((!is_null($year)) && (!is_null($week)) && (!is_null($mealDays)) && (!is_null($crewNames))) {
				CMealsEvent::createEventGroupCMWK($year, $week, CMealsFrontEndUtils::explodeAndTrim($mealDays), CMealsFrontEndUtils::explodeAndTrim($crewNames));
			}
			else {
				CMeals::getErrorHandler()->handleError("INSUFFICIENT_REQUEST", print_r(JRequest::get('post'), true));
			}
		}
		else {
			CMeals::getErrorHandler()->handleError("UNSUPPORTED_CREATE_METHOD", print_r(JRequest::get('post'), true));
		}
		$this->setRedirect("index.php?option=".$option."&view=eventgroups" );
	}
	
	/**
	 * Method to cancel
	 */
	public function cancel() {
		global $option;
		$viewName = JRequest::getVar( 'view', NULL);
		switch ($viewName) {
			case "editeventgroup":
				$this->setRedirect("index.php?option=".$option."&view=eventgroups" );
				break;
		}
	}
	
	/**
	 * Method to display the view
	 * @access public
	 */
	public function display() {
		
		// Do cleanup once every session (bigger cleanup than in site controller)
		$session = JFactory::getSession();
		$cleanupDone = $session->get( "cleanupDone", false, "cmeals" );
		if (!$cleanupDone) {
			CMeals::archiveEvents();
			CMeals::cleanupEventGroups();
			$session->set( "cleanupDone", true, "cmeals" );
		}
		
		$viewName = JRequest::getVar('view', null);
		
		if (is_null($viewName)) {
			$this->setRedirect("index.php?option=com_communalmeals&view=eventgroups");
			$this->redirect();
		}
		
		parent::display();
	}

}